package org.leon;

import org.leon.util.HttpProtocolUtil;
import org.leon.util.StaticResourcesUtil;

import java.io.*;

public class Response {

    private OutputStream outputStream;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public Response() {
    }

    public void outputHtml(String path) throws IOException {
        String absolutePath = StaticResourcesUtil.getAbsolutePath(path);

        File file = new File(absolutePath);
        if (file.exists() && file.isFile()) {
            // 读取文件，放到输出流中
            StaticResourcesUtil.outputStaticResources(new FileInputStream(file), outputStream);
        } else {
            // 输出404
            outputString(HttpProtocolUtil.httpHeader404());
//            outputHtml("/404.html");
        }
    }

    public void outputString(String context) throws IOException {
        this.outputStream.write(context.getBytes());
    }
}
